/*——————————————————————————————封装ajax请求并且将数据渲染到页面表格————————————————————————————————*/
$("#query").click(function(){
	getAjax()
});
function getAjax(){
	var flightNumber=($("#flightNumber").val()).toUpperCase();//航班号 
	var departureDate=$("#departureDate").val();//出发日期
	if(/\((.)+?\)/.test($(".dep").val())){//获取出发机场三字码
		var departure = $(".dep").val().slice(-5).split("(")[1].split(")")[0];
	}else{
		var departure = $(".dep").val()
	}
	if(/\((.)+?\)/.test($(".arr").val())){//获取到达机场三字码
		var arrival = $(".arr").val().slice(-5).split("(")[1].split(")")[0];
	}else{
		var arrival = $(".arr").val()
	}
	//var departure=$(".hiddeDepInput").text();//出发城市
	//var arrival=$(".hiddeArrInput").text();//到达城市
	if(flightNumber==""||departureDate==""){
		layer.msg("请输入完整的查询信息",{icon:2,time:1000})
		return false;
	}else{
		$(".clickMes1").text(flightNumber);
		$(".clickMes2").text(departureDate);
		$(".clickMes3").html(departure+arrival);
		
	}
	$(".clickMes1").text(flightNumber);
	$(".clickMes2").text(departureDate);
	$(".clickMes3").html(departure+arrival);
		
	$.ajax({
		type:"get",
		//url:"../JSON/ticketGenerated.json",
		url:"../ticket/getOrder?flightNumber="+flightNumber+"&departureDate="+departureDate+"&departure="+departure+"&arrival="+arrival+"&temp="+Math.random(), 
		dataType:"json",
		success:function(data){
			$(".clear").empty();
			$(".clickMes5").text(data.data.occupiedSeat+"/"+data.data.totalSeat);//座位完成情况
			$(".clickMes7").text("成人："+data.data.totalAdult+"/儿童:"+data.data.totalChild+"/婴儿:"+data.data.totalBaby);//旅客总数
			$(".clickMes9").text(data.data.noPayOrder+"/"+data.data.payedOrder+"/"+data.data.ticketedOrder);//订单完成情况
			
			var str="";
			var order=data.data.order;//获取表格中的数组list
			for(var i=0;i<order.length;i++){
				var connectImg = "";
				if(order[i].connect==1){
					connectImg+="<span class='connectImg'></span>"
				}
				str+="<tr class='orderTr'>" +
						"<td class='mainMessTd' colspan='9'>"+connectImg+
						"订单号:<span class='faa000 order_link'>"+order[i].id+"</span>" +
						"客户:<span class='f00aa99'>"+order[i].customer+"</span>" +
						"旅客人数:<span class='f00aa99'>"+order[i].seatNumber+"/"+order[i].actualPassengerNumber+"/"+order[i].passengerTotal+"</span></td>" +
						"<td class='mainMessTd2'><button show="+bornPNR(order[i].passengers)+" orderId="+order[i].id+" index="+i+" class='bornPNR'>生成PNR</button><span show=false index="+i+" class='iconfont showHide'>&#xe807;</span></td>" +
					 "</tr>";//订单号 客户 旅客人数
				var len=order[i].passengers.length;//获取旅客信息的数组长度
				if(len!=0){
				for(var j=0;j<order[i].passengers.length;j++){					
				   var name = "";
				   if(commomFun.toEmptyString(order[i].passengers[j].chineseSurname)==""&&commomFun.toEmptyString(order[i].passengers[j].chineseName)!=""){
					   name = order[i].passengers[j].chineseName;
				   }else if(commomFun.toEmptyString(order[i].passengers[j].chineseSurname)!=""){
					   name = commomFun.toEmptyString(order[i].passengers[j].chineseSurname)+commomFun.toEmptyString(order[i].passengers[j].chineseName);
				   }else{
					   name = commomFun.toEmptyString(order[i].passengers[j].englishSurname)+"/"+commomFun.toEmptyString(order[i].passengers[j].englishName);
				   }
				   str+='<tr index='+i+'><td>'+name+"("+commomFun.genderStr(order[i].passengers[j].gender)+")"+"<br/>"+
								commomFun.typeNumStr(order[i].passengers[j].type)+"<br/>"+commomFun.toEmptyString(order[i].passengers[j].birthDate)+
						'</td>'+
						'<td>'+commomFun.certificateTypeStr(order[i].passengers[j].certificateType)+'</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].certificateNumber)+'</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].email)+'</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].issuingCountry)+'</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].country)+'</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].certificateExpireDate)+'</td>'+
						'<td>'+commomFun.roundTypeStr(order[i].passengers[j].roundType)+'</td>'+
						'<td>&nbsp;</td>'+
						'<td>'+commomFun.toEmptyString(order[i].passengers[j].pnr)+'</td>'+
						'</tr>'
					
					}
				}else{//passengers的长度为0的时候
					str+='<tr index='+i+'><td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>N/A</td>'+
					'<td>&nbsp;</td>'+
					'<td>&nbsp;</td>'+
					'<td>&nbsp;</td>'+
					'</tr>'
					
				}
			}
			$(".clear").append(str);	
			disabled()
		}
	});
	downloadUrl = "../ticket/downloadOrderExcel?flightNumber="+flightNumber+"&departureDate="+departureDate+"&departure="+departure+"&arrival="+arrival+"&temp="+Math.random();
}
//__________________________________________________________________________________________生成pnr按钮的显示和隐藏
function bornPNR(passengers){
	var length = passengers.length;
	if(length==0){
		return "hide";
	}else if(passengers[0].pnr){
		return "hide";
	}else{
		return "show";
	}
}
function disabled(){
	var length = $(".bornPNR").length;
	for(var i=0;i<length;i++){
		var bornPNR = $(".bornPNR").eq(i).attr("show");
		if(bornPNR=="hide"){
			$(".bornPNR").eq(i).prop("disabled",true);
		}
	}
}
//__________________________________________________________________________________________提交名单
$(".toNames").click(function(){	
	layer.load();
	$.ajax({
	    url: downloadUrl,
	    type: 'get',
	    success: function(data) {
	    	try{
	    		var data = JSON.parse(data);
	    		layer.closeAll('loading');
	    		layer.alert(data.message);
	    	}catch(e){
	    		window.location = downloadUrl;
	    		layer.closeAll('loading');
	    	}	    		    	  		    		    		    		        
	    },
	    error:function(data){
	    	layer.alert("服务器错误")
	    }
	});	
});
//____________________________________________出发到达机场条件互换________________
$(".exchangeAirportCode").click(function(){
	var exchangeVar = $(".queryDep").val();
	$(".queryDep").val($(".queryArr").val());
	$(".queryArr").val(exchangeVar);
})


/*——————————————————————————————点击订单号进入订单详情————————————————————————————————*/
$('.tableData').delegate(".order_link","click",function(){	
	var id=$(this).text();//获取当前订单状态的id值	
	layer.open({
		type: 2,
		title: false,
		shadeClose: true,
		closeBtn: 0,
		shade: [0.9,'#cfcfcf'],
		area: ['98%', '98%'],  
		content: ['../ak/embedDiv.html?id='+id],// iframe的url，no代表不显示滚动条
		end:function(){
			
		}
			
	});
 })
//_____________________________________________________________________________showHide
 	
 	
 	$(".clear").delegate(".showHide","click",function(){
 		var index = $(this).attr("index");
 		var show = $(this).attr("show");
 		if(show=="true"){
 			$(".clear tr[index="+index+"]").hide(300);
 			$(this).html("&#xe807;");
 			$(this).attr("show","false");
 		}
 		if(show=="false"){
 			$(".clear tr[index="+index+"]").show(300);
 			$(this).html("&#xe806;");
 			$(this).attr("show","true");
 		}
 		
 	})
//_____________________________________________________________________________生成PNR
 	$(".clear").delegate(".bornPNR","click",function(){
 		var index = $(this).attr("index");
 		var orderId = $(this).attr("orderId")
 		layer.open({
			type: 2,
			title: ['生成PNR','background-color:#6d6e72; color:#fff;font-size:14px;'],
			shadeClose: true,
			shade: [0.9,'#cfcfcf'],
			area: ['500px', '500px'],  
			content: ['../ak/bornPNR.html?orderId='+orderId],// iframe的url，no代表不显示滚动条
			end:function(){
				getAjax();
			}
				
		});
 	})
 //____________________________________________________________________________机场联想
			var autocompleteData=[];
			$.ajax({
				type:"get",
				url:"../Airapt?pageSize=100000&pageNum=1&temp="+Math.random(),
				dataType:"json",
				success:function(data){	
					autocompleteData = data.data.list; 
					$('.autocomplete').autocomplete({
					    lookup: autocompleteData,
					    autoSelectFirst:true,
					    onSelect: function (suggestion) {		   
					    }
					});
				}
			})

